const {
  MarketingModel
} = require('../../../../utils/marketing-model.js');
const marketingModel = new MarketingModel();
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    modelShow: false,
    isShow: true,
    shopName: '',
    address: '',
    shopImg: '',
    tabIdx: 0,
    tabs: [{
        label: '游戏币',
        value: 0
      }
      // { label: '游戏券', value: 1 },
      // { label: '币券套餐', value: 2 }
    ],
    mealList: []
  },
  closeModel() {
    this.setData({
      modelShow: false
    })
  },
  getMealList() {
    let merchantId = wx.getStorageSync('merchantId')
    marketingModel.firmwareMealList({
      merchantId: merchantId,
      machineInstanceSlotId: this.data.machineInstanceSlotId
    }).then(data => {
      const mealList = (data.machineProductDTOList || []).map(item => {
        const split = item.price.toFixed(2).split('.')
        return {
          ...item,
          img: item.img,
          productName: item.productName,
          int: split[0],
          float: split[1]
        }
      })
      this.setData({
        mealList,
        shopName: data.shopInfo.shopName || '',
        address: data.shopInfo.address || '',
        shopImg: data.shopInfo.shopImage || '',
        modelShow: true
      })
    }).catch(err => {
      if (err.status === 400) {
        wx.setNavigationBarTitle({
          title: '抱歉，机台被占用'
        })
        this.setData({
          isShow: false
        })
      } else {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  goCommitOrder(e) {
    let meal = e.currentTarget.dataset.meal
    wx.navigateTo({
      url: `/pages/scanCode/firmware/commitOrder/index?shopName=${this.data.shopName}&meal=${JSON.stringify(meal)}&machineInstanceSlotId=${this.data.machineInstanceSlotId}`
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that=this
    that.setData({
      machineInstanceSlotId: options.machineInstanceSlotId
    }, () => {
      if (wx.getStorageSync('merchantId')) {
        that.getMealList()
      } else {
        app.getInfo().then(data => {
          that.getMealList()
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})